package com.example.lanouhn.firstapp.activity.activity;


import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lanouhn.firstapp.R;
import com.example.lanouhn.firstapp.activity.adapter.GoodsDetailPagerAdapter;
import com.example.lanouhn.firstapp.activity.fragment.IntoGoodsCommentFragmen;
import com.example.lanouhn.firstapp.activity.fragment.IntoGoodsDetailFragment;
import com.example.lanouhn.firstapp.activity.fragment.IntoGoodsGoodsFragment;
import com.example.lanouhn.firstapp.activity.model.Syomb;
import com.example.lanouhn.firstapp.activity.utils.HttpUtils;
import com.example.lanouhn.firstapp.activity.utils.SPUtils;
import com.example.lanouhn.firstapp.activity.utils.SyombJson;
import com.example.lanouhn.firstapp.activity.utils.T;
import com.example.lanouhn.firstapp.activity.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品详情页面的布局
 * 利用tablayout来做标签栏
 * 实现viewpager的滑动效果
 * 绑定三个Fragment分别为 ：商品、详情、评价
 */
public class GoodsDetailsActivity extends FragmentActivity {

    //定义变量
    private TabLayout tabLayout;
    private ViewPager viewPager;
    //定义集合  传递标签名称
    private List<String> list_title;
    private List<Fragment> fragmentList;

    private GoodsDetailPagerAdapter adapter;

    //接收上个Activity传递过来的id
    private String goods_id;

    //返回键
    private ImageView back;
    //购物车点击事件
    private ImageView shoppingCar;

    //加入购物车
    private TextView into_car;
    //立即购买
    private TextView shopping_now;

    //关注点击事件
    private RelativeLayout rll_attention;

    private String key;
    private Syomb slist;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_details);

        rll_attention = (RelativeLayout) findViewById(R.id.rll_attention);
        //获取上个页面传过来的参数
         goods_id = getIntent().getStringExtra("goods_id");



         key = (String) SPUtils.get(GoodsDetailsActivity.this,"log_key","");

        //用来向Fragment中传值
        getIntent().putExtra("str",goods_id);

        //找控件
        initview();
        //封装数据
        initData();
        //绑定数据到控件
        setDataToView();
        //点击事件
        setOnClickListener();

    }

    private void setOnClickListener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //购物车图标的点击事件
        shoppingCar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(GoodsDetailsActivity.this,MainActivity.class);
                intent.putExtra("symbol",3);
                startActivity(intent);
            }
        });

        //加入购物车点击事件
        into_car.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //发送广播，在详情页面接收  执行购物车点击事件
                Intent intent = new Intent();
                intent.setAction("into_car");
                sendBroadcast(intent);
            }
        });
        //立即购买的点击事件
        shopping_now.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(GoodsDetailsActivity.this,CheckInformationActivity.class);
              startActivity(intent);
            }

        });



        //关注点击事件
        rll_attention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        String collectionGoods_url = "http://www.niaolema.com/mobile/index.php?act=member_favorites&op=favorites_add";

                        String params ="goods_id=" + goods_id + "&key=" + key ;
                        String result = HttpUtils.doPost(collectionGoods_url,params);
                        if(!result.equals("") && !result.equals("null")) {
                            slist = SyombJson.getSyomb(result);


                            if (slist.getCode() == 200 && slist.getDatas() == 1) {
                                //获取数据成功
                                handler.sendEmptyMessage(1);
                            } else {
                                handler.sendEmptyMessage(0);
                            }

                        }else {
                            handler.sendEmptyMessage(0);
                        }
                    }
                }).start();

            }
        });


    }

    //线程
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){

                case 0:
                    T.showShort(GoodsDetailsActivity.this,"收藏失败，请重试");
                break;
                case 1:
                    ToastUtils.showToast(
                            getApplicationContext(), R.mipmap.xiao_lan, "收藏成功");
                break;
                default:
                    break;
            }
        }
    };


    /**
     * 绑定数据到控件
     */
    private void setDataToView() {
        adapter = new GoodsDetailPagerAdapter(getSupportFragmentManager(), list_title, fragmentList);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

    /**
     * 添加数据源
     */
    private void initData() {

        list_title = new ArrayList<>();
        list_title.add("商品");
        list_title.add("详情");
        list_title.add("评价");

        fragmentList = new ArrayList<>();
        fragmentList.add(new IntoGoodsGoodsFragment());
        fragmentList.add(new IntoGoodsDetailFragment());
        fragmentList.add(new IntoGoodsCommentFragmen());
    }

    /**
     * 初始化控件
     */
    private void initview() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        back = (ImageView) findViewById(R.id.imageview_left);
        shoppingCar = (ImageView) findViewById(R.id.car);
        into_car = (TextView) findViewById(R.id.into_car);
        shopping_now = (TextView) findViewById(R.id.shopping_now);
    }


}


